<%--
  Created by IntelliJ IDEA.
  User: LC
  Date: 2019/1/17
  Time: 20:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
<html>
<!-- ZTree树形插件 --><%--
  Created by IntelliJ IDEA.
  User: LC
  Date: 2018/12/8
  Time: 14:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <title>权限管理</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
</head>
<body>
<table class="layui-hide" id="article" lay-filter="article_bar"></table>
<script type="text/html" id="barArticle">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <shiro:hasPermission name="role:del">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
    </shiro:hasPermission>
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="addPerGroup">
            <i class="layui-icon layui-icon-add-circle"></i>新增权限组
        </button>
        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="addPer">
            <i class="layui-icon layui-icon-add-circle"></i>新增权限
        </button>
    </div>
</script>
</body>
</html>
<script src="/static/layui/layui.js"></script>
<script>
    layui.use(['laydate', 'laypage', 'layer', 'table', 'element','jquery'], function(){
        var laydate = layui.laydate //日期
            ,laypage = layui.laypage //分页
            ,layer = layui.layer //弹层
            ,table = layui.table //表格
            ,element = layui.element //元素操作
            ,$ = layui.jquery //jquery


        //监听Tab切换
        element.on('tab(article)', function(data){
            layer.tips('切换了 '+ data.index +'：'+ this.innerHTML, this, {
                tips: 1
            });
        });

        //执行一个 table 实例
        table.render({
            elem: '#article'
            ,height: 'full-30'
            ,url: '/api/per/list' //数据接口
            ,title: '用户表'
            ,limits:[20,30,50,100]
            ,limit : 20
            ,page: true //开启分页
            ,toolbar: '#toolbar' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}//表头
                ,{field: 'id', title: 'ID', width:120, sort: true, fixed: 'left',align:'center'}
                ,{field: 'name', title: '名称', width:200, align:'center'}
                ,{field: 'sn', title: '标识', width:200, align:'center'}
                ,{field: 'resource', title: '资源', width:200, align:'center'}
                ,{field: 'pId', title: '父级权限id', width:200, align:'center'}
                ,{fixed: 'right',title: '操作', width: 265, align:'center', toolbar: '#barArticle'}
            ]]
        });

        //监听头工具栏事件
        table.on('toolbar(article_bar)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'addPerGroup':
                    layer.open({
                        type: 2, //type:0 也行
                        title: "新增权限组",
                        area: ["400px", "350px"],
                        content: '/permission/addPerGroup',
                    });
                    break;
                case 'addPer':
                    layer.open({
                        type: 2, //type:0 也行
                        title: "新增权限",
                        area: ["400px", "350px"],
                        content: '/permission/addPer',
                    });
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(article_bar)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'detail'){
                layer.msg('查看操作');
            } else if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    //向服务端发送删除指令
                    deleteUser(obj.data.id);
                    table.reload();
                });
            } else if(layEvent === 'edit'){
                // layer.open({
                //     type: 2, //type:0 也行
                //     title: "授权",
                //     area: ["400px", "80%"],
                //     content: '/per?id='+data.id,
                //     btn: ['确定','取消']
                //     ,yes: function(index, layero){
                //         //获取iframe的body元素
                //         var body = layer.getChildFrame('body',index);
                //         //得到iframe页的窗口对象
                //         var iframeWin = window[layero.find('iframe')[0]['name']];
                //         //执行iframe页的showMsg方法
                //         iframeWin.get();
                //     }
                // });
            }
        });
        function deleteUser(id)
        {
            $.ajax({
                url:"/user/delete/"+id,
                success:function (res) {
                    console.log(res)
                }
            })
        }

    });
</script>
